package com.yinian.cart.service.CartList;



import com.yinian.cart.entity.CartList;
import com.yinian.cart.entity.OrderList;
import com.yinian.cart.repository.CartListRepository;
import com.yinian.cart.repository.OrderListRepository;
import jakarta.transaction.Transactional;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
@Transactional
public class CartListServiceImpl implements CartListService{
    @Autowired
    private CartListRepository cartListRepository;
    @Autowired
    private OrderListRepository orderListRepository;
    @Override
    public CartList addNewCartList(String productId, int productCount, int userAccount) {
        CartList answer = cartListRepository.findByProductIdAndUserAccount(productId,userAccount);
        if (answer!=null){
            answer.setProductCount(answer.getProductCount()+productCount);
        }else{
            answer = new CartList();
            answer.setCartId(UUID.randomUUID().toString());
            answer.setUserAccount(userAccount);
            answer.setProductCount(productCount);
            answer.setProductId(productId);
            answer.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        CartList cartList = cartListRepository.save(answer);
        return cartList;
    }

    @Override
    public JSONArray findAllCartListByUserAccount(int userAccount) {
        List<CartList> cartListList = cartListRepository.findAllByUserAccountOrderByAddTimeAsc(userAccount);
        JSONArray jsonArray = new JSONArray();
        for(CartList cartList : cartListList){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cartId",cartList.getCartId());
            jsonObject.put("productId",cartList.getProductId());
            jsonObject.put("getProductCount",cartList.getProductCount());
            jsonArray.put(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public JSONObject ChangeCart(String cartId, int productCount, int userAccount) {
        CartList cartList = cartListRepository.findByCartIdAndUserAccount(cartId,userAccount);
        CartList cartList1 = null;
        if (cartList!=null){
            cartList.setProductCount(productCount);
            cartList1 = cartListRepository.save(cartList);
        }
        JSONObject jsonObject = new JSONObject();
        if(cartList1!=null){
            jsonObject.put("status","ok");
        }else{
            jsonObject.put("status","no");
        }
        return jsonObject;
    }

    @Override
    public String setOrder(JSONArray jsonArray) {
        String dataId = "";
        if (jsonArray.length()!=0) {
            dataId = UUID.randomUUID().toString();
            for (int i=0;i<jsonArray.length();i++){
//            每个dataId不一样,确保没中数据不同
                OrderList orderList = new OrderList();
                orderList.setDataId(UUID.randomUUID().toString());
//            同一个订单里的orderId一样,确保能找到一个订单中的数据
                orderList.setOrderId(dataId);
                orderList.setOrderPrice(jsonArray.getJSONObject(i).getDouble("productPrice"));
                orderList.setProductCount(jsonArray.getJSONObject(i).getInt("productCount"));
                orderList.setUserAccount(jsonArray.getJSONObject(i).getInt("userAccount"));
                orderList.setProductId(jsonArray.getJSONObject(i).getString("productId"));
                orderList.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                orderListRepository.save(orderList);
                cartListRepository.deleteByCartId(jsonArray.getJSONObject(i).getString("cartId"));
            }
        }
        return dataId;
    }

    @Override
    public int deleteCartListByCartId(String cartId) {
        int key = cartListRepository.deleteByCartId(cartId);
        return key;
    }
}
